<template>
  <div class="view">
    <Panel :title="title">
      <el-form label-position="top">
        <el-row :gutter="20">
          <el-col :span="10">
            <el-form-item label="课程名称" required>
              <el-input v-model="course.name" placeholder="请输入课程名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="9" :offset="3">
            <el-form-item label="可见状态" required>
              <el-switch v-model="course.visible"></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="9">
            <el-form-item label="课程简介" required>
              <Simditor v-model="course.description"></Simditor>
            </el-form-item>
          </el-col>
          <el-col :span="9" :offset="3">
            <el-form-item label="内容概要" required>
              <Simditor v-model="course.content"></Simditor>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="9">
            <el-form-item label="教学目标" required>
              <Simditor v-model="course.goal"></Simditor>
            </el-form-item>
          </el-col>
          <el-col :span="9" :offset="3">
            <el-form-item label="参考资源" required>
              <Simditor v-model="course.references"></Simditor>
            </el-form-item>
          </el-col>
        </el-row>
        </el-form>
        <el-row>
          <el-col :offset="10">
            <save @click.native="saveCourse"></save>
          </el-col>
        </el-row>
    </Panel>
  </div>
</template>

<script>
import api from '../../api.js'
// Simditor是写好的编辑栏组件，我们可以直接引入使用
import Simditor from '../../components/Simditor.vue'

export default {
  name: 'CreateCourse',
  components: { Simditor },
  data () {
    return {
      title: '添加课程',
      course: {
        name: '',
        description: '',
        goal: '',
        content: '',
        references: '',
        visible: false
      }
    }
  },
  mounted () {
    if (this.$route.name === 'edit-course') {
      this.title = '编辑课程'
      api.getCourse(this.$route.params.courseId).then(res => {
        this.course = res.data.data
      }).catch(() => {
      })
    }
  },
  methods: {
    saveCourse () {
      let funcName = this.$route.name === 'edit-course' ? 'editCourse' : 'createCourse'
      let data = Object.assign({}, this.course)
      console.log(data)
      /* api[funcName](data).then(res => {
        this.$router.push({name: 'course-list', query: {refresh: 'true'}})
      }).catch((e) => {
      }) */
    }
  }
}
</script>
